#include <intrins.h>
#include "user_config.h"
#include "display.h"
#include "driver/st7735.h"
#include <stdio.h>


#define DISPLAY_WIDTH			80
#define DISPLAY_HEIGHT		160








#include "images/image_d1.bmp.h"
#include "images/image_d2.bmp.h"
#include "images/image_d3.bmp.h"
#include "images/image_d4.bmp.h"
#include "images/image_d5.bmp.h"
#include "images/image_d6.bmp.h"
#include "images/image_d7.bmp.h"
#include "images/image_d8.bmp.h"
#include "images/image_logo.bmp.h"
#include "images/image_SR_white.bmp.h"
#include "images/image_SR_grey.bmp.h"
#include "images/image_HR_white.bmp.h"
#include "images/image_HR_grey.bmp.h"
#include "images/image_auto.bmp.h"
#include "images/image_manual.bmp.h"
#include "images/image_ice.bmp.h"
#include "images/image_hot.bmp.h"
#include "images/image_battery_0.bmp.h"
#include "images/image_battery_white_1.bmp.h"
#include "images/image_battery_white_2.bmp.h"
#include "images/image_battery_white_3.bmp.h"
#include "images/image_battery_white_4.bmp.h"
#include "images/image_battery_white_5.bmp.h"
#include "images/image_battery_yellow_1.bmp.h"
#include "images/image_battery_yellow_2.bmp.h"
#include "images/image_battery_yellow_3.bmp.h"
#include "images/image_battery_yellow_4.bmp.h"
#include "images/image_battery_yellow_5.bmp.h"
#include "images/image_battery_red_1.bmp.h"
#include "images/image_battery_red_2.bmp.h"
#include "images/image_battery_red_3.bmp.h"
#include "images/image_battery_red_4.bmp.h"
#include "images/image_battery_red_5.bmp.h"
#include "images/image_turbo0.bmp.h"
#include "images/image_turbo1.bmp.h"
#include "images/image_turbo2.bmp.h"
#include "images/image_ball_red.bmp.h"
#include "images/image_ball_magenta.bmp.h"
#include "images/image_ball_blue.bmp.h"
#include "images/image_ball_green.bmp.h"
#include "images/image_ball_yellow.bmp.h"
#include "images/image_closing.bmp.h"
#include "images/image_lock.bmp.h"



typedef struct image_item
{
	unsigned char *				address;
	unsigned char					x;
	unsigned char					y;
	unsigned char					w;
	unsigned char					h;
}image_item;




//const image_item	g_images[] = {
//	{image_d1,								31,		97,		20,		30},
//	{image_d2,								31,		97,		20,		30},
//	{image_d3,								31,		97,		20,		30},
//	{image_d4,								31,		97,		20,		30},
//	{image_d5,								31,		97,		20,		30},
//	{image_d6,								31,		97,		20,		30},
//	{image_d7,								31,		97,		20,		30},
//	{image_d8,								31,		97,		20,		30},
//	{image_logo,							16,		1,		47,		158},
//	{image_SR_white,					20,		8,		14,		8},
//	{image_SR_grey,						20,		8,		14,		8},
//	{image_HR_white,					2,		8,		17,		8},
//	{image_HR_grey,						2,		8,		17,		8},
//	{image_auto,							1,		143,	45,		9},
//	{image_manual,						1,		143,	45,		9},
//	{image_ice,								55,		132,	23,		26},
//	{image_hot,								55,		132,	23,		26},
//	{image_battery_0,					49,		5,		29,		13},
//	{image_battery_white_1,		49,		5,		29,		13},
//	{image_battery_white_2,		49,		5,		29,		13},
//	{image_battery_white_3,		49,		5,		29,		13},
//	{image_battery_white_4,		49,		5,		29,		13},
//	{image_battery_white_5,		49,		5,		29,		13},
//	{image_battery_yellow_1,	49,		5,		29,		13},
//	{image_battery_yellow_2,	49,		5,		29,		13},
//	{image_battery_yellow_3,	49,		5,		29,		13},
//	{image_battery_yellow_4,	49,		5,		29,		13},
//	{image_battery_yellow_5,	49,		5,		29,		13},
//	{image_battery_red_1,			49,		5,		29,		13},
//	{image_battery_red_2,			49,		5,		29,		13},
//	{image_battery_red_3,			49,		5,		29,		13},
//	{image_battery_red_4,			49,		5,		29,		13},
//	{image_battery_red_5,			49,		5,		29,		13},
//	{image_turbo0,						23,		41,		38,		38},
//	{image_turbo1,						23,		41,		38,		38},
//	{image_turbo2,						23,		41,		38,		38},
//	{image_turbo0,						23,		55,		38,		38},
//	{image_turbo1,						23,		55,		38,		38},
//	{image_turbo2,						23,		55,		38,		38},	
//	{image_ball_yellow,				0,		0,		9,		9},		
//	{image_ball_green,				0,		0,		9,		9},	
//	{image_ball_blue,					0,		0,		9,		9},	
//	{image_ball_magenta,			0,		0,		9,		9},	
//	{image_ball_red,					0,		0,		9,		9},
//	{image_closing,						25,		51,		30,		10},
//	{image_lock,							37,		6,		10,		11},
//	
//};


const image_item	g_images[] = {
	{image_d1,								30,		95,		22,		32},
	{image_d2,								30,		95,		22,		32},
	{image_d3,								30,		95,		22,		32},
	{image_d4,								30,		95,		22,		32},
	{image_d5,								30,		95,		22,		32},
	{image_d6,								30,		95,		22,		32},
	{image_d7,								30,		95,		22,		32},
	{image_d8,								30,		95,		22,		32},
	{image_logo,							16,		1,		47,		158},
	{image_SR_white,					20,		8,		14,		8},
	{image_SR_grey,						20,		8,		14,		8},
	{image_HR_white,					2,		8,		17,		8},
	{image_HR_grey,						2,		8,		17,		8},
	{image_auto,							1,		143,	45,		9},
	{image_manual,						1,		143,	45,		9},
	{image_ice,								55,		132,	23,		26},
	{image_hot,								55,		132,	23,		26},
	{image_battery_0,					49,		5,		29,		13},
	{image_battery_white_1,		49,		5,		29,		13},
	{image_battery_white_2,		49,		5,		29,		13},
	{image_battery_white_3,		49,		5,		29,		13},
	{image_battery_white_4,		49,		5,		29,		13},
	{image_battery_white_5,		49,		5,		29,		13},
	{image_battery_yellow_1,	49,		5,		29,		13},
	{image_battery_yellow_2,	49,		5,		29,		13},
	{image_battery_yellow_3,	49,		5,		29,		13},
	{image_battery_yellow_4,	49,		5,		29,		13},
	{image_battery_yellow_5,	49,		5,		29,		13},
	{image_battery_red_1,			49,		5,		29,		13},
	{image_battery_red_2,			49,		5,		29,		13},
	{image_battery_red_3,			49,		5,		29,		13},
	{image_battery_red_4,			49,		5,		29,		13},
	{image_battery_red_5,			49,		5,		29,		13},
	{image_turbo0,						23,		41,		38,		38},
	{image_turbo1,						23,		41,		38,		38},
	{image_turbo2,						23,		41,		38,		38},
	{image_turbo0,						23,		55,		38,		38},
	{image_turbo1,						23,		55,		38,		38},
	{image_turbo2,						23,		55,		38,		38},	
	{image_ball_yellow,				0,		0,		9,		9},		
	{image_ball_green,				0,		0,		9,		9},	
	{image_ball_blue,					0,		0,		9,		9},	
	{image_ball_magenta,			0,		0,		9,		9},	
	{image_ball_red,					0,		0,		9,		9},
	{image_closing,						25,		51,		30,		10},
	{image_lock,							37,		6,		10,		11},
	
};



static unsigned char g_locate[8][4] = {
{18,45,9,9},
{31,36,9,9},
{43,36,9,9},
{55,45,9,9},
{55,59,9,9},
{43,68,9,9},
{31,68,9,9},
{18,59,9,9},
};





void display_init()
{
	st7735_init();
}

void display_on()
{
	LCD_BKLIGHT_ON();
}

void display_off()
{
	LCD_BKLIGHT_OFF();
}

void display_clear(unsigned int color)
{
	st7735_fill(color, 0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT);
}

void display_show(unsigned char id)
{
	st7735_bitblt((unsigned int *)g_images[id].address, g_images[id].x, g_images[id].y, g_images[id].w, g_images[id].h);
}

void display_hide(unsigned char id)
{
	st7735_fill(COLOR_DEFAULT, g_images[id].x, g_images[id].y, g_images[id].w, g_images[id].h);
}


void display_animation_show(unsigned char ball_step, unsigned char digit_step)
{
	unsigned char parts[8] = {0};
	unsigned char i;
	
	//show balls step
	for(i=0;i<5;i++)
	{
		parts[(ball_step + i)%8] = IMAGE_BALL_YELLOW + i;
	}
	
	for(i=0;i<8;i++)
	{
		if(parts[i] == 0)
		{
			st7735_fill(COLOR_DEFAULT, g_locate[i][0], g_locate[i][1], g_locate[i][2], g_locate[i][3]);
		}
		else
		{
			st7735_bitblt((unsigned int *)g_images[parts[i]].address, g_locate[i][0], g_locate[i][1], g_locate[i][2], g_locate[i][3]);
		}
	}
	
	//show closing
	st7735_bitblt((unsigned int *)g_images[IMAGE_CLOSING].address, g_images[IMAGE_CLOSING].x, g_images[IMAGE_CLOSING].y, g_images[IMAGE_CLOSING].w, g_images[IMAGE_CLOSING].h);
	
	//show digits step
	display_show(digit_step);
}

void display_animation_hide()
{
	unsigned char i;
	
	//show balls step
	for(i=0;i<8;i++)
	{
		st7735_fill(COLOR_DEFAULT, g_locate[i][0], g_locate[i][1], g_locate[i][2], g_locate[i][3]);
		i++;
	}
	
	st7735_fill(COLOR_DEFAULT, g_images[IMAGE_CLOSING].x, g_images[IMAGE_CLOSING].y, g_images[IMAGE_CLOSING].w, g_images[IMAGE_CLOSING].h);
	
	display_hide(0);
}

